package dados.persistencias;

import java.util.List;

import javax.persistence.TypedQuery;

import negocio.erro.DaoException;
import basica.academico.Disciplina;
import basica.administrativo.Pessoa;
import dados.geral.DAOGenerico;
import dados.interfaces.IDAODisciplina;

public class DAODisciplina extends DAOGenerico<Disciplina> implements IDAODisciplina{

	@Override
	public Boolean pesquisarVinculoCampusFuncionario(Integer codigo){
		TypedQuery<Pessoa> query = entityManager.createQuery("from Pessoa p where p.campus.codigo = :codigo", Pessoa.class);
		query.setParameter("codigo", codigo);
		
		if(query.getResultList().isEmpty()){
			return false;
		}
		return true;
	}
	
	@Override
	public List<Disciplina> listarTodosJoin()
			throws DaoException {
		TypedQuery<Disciplina> query = entityManager.createQuery(
				"select disciplina from Disciplina disciplina join fetch disciplina.preRequisito", Disciplina.class);
		//entityManager.getEntityManagerFactory().getCache().evictAll();
		return query.getResultList();
	}
	
	@Override
	public List<Disciplina> pesquisarDisciplinaPorNome(String nome) throws DaoException {
		TypedQuery<Disciplina> query = entityManager.createQuery(
				"from Disciplina disc where disc.nome like :nome", Disciplina.class);
		query.setParameter("nome", "%" + nome + "%");
		return query.getResultList();
	}

	@Override
	public List<Disciplina> pesquisarDisciplinaPorCodigo(Integer codigo) throws DaoException {
		TypedQuery<Disciplina> query = entityManager.createQuery(
				"from Disciplina disc where disc.codigo = :codigo", Disciplina.class);
		query.setParameter("codigo", "%" + codigo + "%");
		return query.getResultList();
	}

}
